package com.dgut.finaldemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.finaldemo.entity.User;
import com.dgut.finaldemo.mapper.UserMapper;
import com.dgut.finaldemo.vo.DataVO;
import com.dgut.finaldemo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public DataVO<User> findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        IPage<User> userIPage = new Page<>(page,limit);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("admin",0);
        IPage<User> result = userMapper.selectPage(userIPage,queryWrapper);

        List<User> userList = result.getRecords();
        List<UserVO> userVOList = new ArrayList<>();
        dataVO.setCount(result.getTotal());
        userList.forEach(System.out::println);

        for (User user:userList){
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user,userVO);
            if(user.getAdmin()==0){
                userVO.setAdmin("普通管理员");
            }
            if(user.getStatus()==1){
                userVO.setStatus("正常");
            }else{
                userVO.setStatus("停用");
            }
            if(user.getAdmin()==0)
                userVOList.add(userVO);
        }

        dataVO.setData(userVOList);
        return dataVO;
    }

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public User queryUserById(Integer uid) {
        User user = userMapper.selectById(uid);
        return user;
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void deleteUserById(Integer uid) {
        userMapper.deleteById(uid);
    }

    @Override
    public DataVO<User> searchUser(Integer page, Integer limit, Integer uid,String username, String password) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        if(username==null)
            username = "";
        if(password==null)
            password= "";

        IPage<User> userIPage = new Page<>(page,limit);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(uid==null){
            queryWrapper
                    .eq("admin",0)
                    .like("username",username)
                    .like("password",password);
        }else{
            queryWrapper
                    .eq("admin",0)
                    .eq("uid",uid)
                    .like("username",username)
                    .like("password",password);
        }

        IPage<User> result = userMapper.selectPage(userIPage,queryWrapper);

        List<User> userList = result.getRecords();
        List<UserVO> userVOList = new ArrayList<>();
        dataVO.setCount(result.getTotal());
        userList.forEach(System.out::println);

        for (User user:userList){
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user,userVO);
            if(user.getAdmin()==0){
                userVO.setAdmin("普通管理员");
            }
            if(user.getStatus()==1){
                userVO.setStatus("正常");
            }else{
                userVO.setStatus("停用");
            }
            if(user.getAdmin()==0)
                userVOList.add(userVO);
        }

        dataVO.setData(userVOList);
        return dataVO;
    }

    @Override
    public User queryUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }
}
